package com.uptown.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author: Lixiaofei
 * @date: 2020/5/8 16:57
 */
public class NioFileChannel {

    public static void main(String[] args) throws IOException {
        String str = "fighting! Mr Lee!";
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("e:\\file01.txt");
        //通过stream获取文件fileChannel
        FileChannel channel = fileOutputStream.getChannel();
        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //放入sr
        byteBuffer.put(str.getBytes());
        //反转buffer
        byteBuffer.flip();
        channel.write(byteBuffer);
    }

}
